package com.xj.controller;

import com.xj.model.Book;
import com.xj.model.User;
import com.xj.service.BookService;
import com.xj.service.BrowerHolderService;
import com.xj.utils.BrowerHolderUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Created by xiongjie on 2020/3/1.
 */
@Controller
public class BookController {

    //spring自动注入
    @Autowired
    private BookService bookService;

    @Autowired
    private BrowerHolderService browerHolderService;

    /**
     * 项目首页路由，加载数据并跳转到图书列表页面。
     *
     * @param model
     * @return
     */
    @RequestMapping(path = {"/index"}, method = {RequestMethod.GET})
    public String bookList(Model model) {
        try {
            User host = browerHolderService.getHostUser();
            if (host != null) {
                model.addAttribute("host", host);
            }

            List<Book> books = bookService.selectAll();
            model.addAttribute("books", books);
            return "book/books";
        } catch (Exception e) {
            model.addAttribute("error", e.getMessage());
            return "400";
        }
    }

    /**
     * 添加book的路由，直接跳转到页面，不带参数
     *
     * @return
     */
    @RequestMapping(path = {"/book/addbook"}, method = {RequestMethod.GET})
    public String addBook() {
        return "book/addbook";
    }

    /**
     * 添加booke的的具体实现，传入的参数需要使用@RequestParam注解对应的值
     * 操作完成最后进行页面转发
     *
     * @param name
     * @param author
     * @param price
     * @return
     */
    @RequestMapping(path = {"/book/addbook/do"}, method = {RequestMethod.POST})
    public String doAddBook(Model model, @RequestParam("name") String name, @RequestParam("author") String author, @RequestParam("price") String price) {
        try {
            Book book = new Book();
            book.setName(name);
            book.setAuthor(author);
            book.setPrice(price);
            bookService.addBook(book);
            return "redirect:/index";
        } catch (Exception e) {
            model.addAttribute("error", e.getMessage());
            return "400";
        }
    }

    /**
     * 删除book，使用路径参数@PathVariable
     * <p>
     * 路由映射可以使用正则表达式[0-9]+表示数字
     *
     * @param bookId
     * @return
     */
    @RequestMapping(path = {"/book/{bookId:[0-9]+}/delete"}, method = {RequestMethod.GET})
    public String deleteBook(Model model, @PathVariable("bookId") int bookId) {
        try {
            bookService.deleteBook(bookId);
            return "redirect:/index";
        } catch (Exception e) {
            model.addAttribute("error", e.getMessage());
            return "400";
        }
    }

    @RequestMapping(path = {"/book/{bookId:[0-9]+}/recover"}, method = {RequestMethod.GET})
    public String recoverBook(Model model, @PathVariable("bookId") int bookId) {
        try {
            bookService.recoverBook(bookId);
            return "redirect:/index";
        } catch (Exception e) {
            model.addAttribute("error", e.getMessage());
            return "400";
        }
    }
}
